#!/usr/bin/python
# -*- coding: utf-8 -*-

import XMLElement
import unittest

class TestXMLElement(unittest.TestCase):
    """Unit test for XMLElement"""
    
    def setUp(self):
        self.doc = XMLElement.fromFile("sample.xml")
        
    def test_tags(self):
        self.assertEqual(self.doc.sectionA._tag, "sectionA")
        self.assertEqual(self.doc.sectionB._tag, "sectionB")
        self.assertEqual(self.doc.sectionC._tag, "sectionC")
        self.assertEqual(self.doc.sectionA.itemA._tag, "itemA")
        self.assertEqual(self.doc.sectionA.itemB._tag, "itemB")
        
    def test_values(self):
        self.assertEqual(self.doc.sectionA.itemA.text, "this is item a")
        self.assertEqual(self.doc.sectionA.itemB.text, "this is item b")
        self.assertEqual(self.doc.sectionB.item, "2")
        self.assertEqual(self.doc.sectionC.firstName, "Hello")
        self.assertEqual(self.doc.sectionC.lastName, "World")
        
    def test_attibutes(self):
        self.assertEqual(self.doc.sectionA.itemA.id, "a")
        self.assertEqual(self.doc.sectionA.itemB.id, "b")
        
    def test_tag_indexing(self):
        self.assertEqual(self.doc["sectionA"], self.doc.sectionA)
        self.assertEqual(self.doc["sectionB"], self.doc.sectionB)
        self.assertEqual(self.doc["sectionC"], self.doc.sectionC)
        self.assertEqual(self.doc["sectionA"]["itemA"], self.doc.sectionA.itemA)
        self.assertEqual(self.doc["sectionA"]["itemB"], self.doc.sectionA.itemB)
        self.assertEqual(self.doc["sectionB"]["item"], self.doc.sectionB.item)
        self.assertEqual(self.doc["sectionC"]["firstName"], self.doc.sectionC.firstName)
        self.assertEqual(self.doc["sectionC"]["lastName"], self.doc.sectionC.lastName)

    def test_pos_indexing(self):
        self.assertEqual(self.doc[0], self.doc.sectionA)
        self.assertEqual(self.doc[1], self.doc.sectionB)
        self.assertEqual(self.doc[2], self.doc.sectionC)
        self.assertEqual(self.doc[0][0], self.doc.sectionA.itemA)
        self.assertEqual(self.doc[0][1], self.doc.sectionA.itemB)
        self.assertEqual(self.doc[1][1], self.doc.sectionB.item)
        self.assertEqual(self.doc[2][0], self.doc.sectionC.firstName)
        self.assertEqual(self.doc[2][1], self.doc.sectionC.lastName)
        
    def test_list(self):
        self.assertEqual(list(self.doc.sectionB), ["1", "2"])
        
if __name__ == "__main__":   
    unittest.main()
